﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;
namespace CommonLib
{
	public class DocumentResourceManager : ResourceManagerBase<DocumentResourceManager>, IInitFromConfig
	{
		protected DocumentResourceManager() :base("Document")
		{
			IsInit = true;
			_ConfigInit(Const.Map_Document);
		}

		public string GetDocumentPathByName(string name)
		{
			if (dicPathMap.TryGetValue(name, out string path))
				return path;
			else
				return "";
		}



		public void _ConfigInit(string _fileName)
		{
			string jsonString = "";
			string path = Path.Combine(RootPath, _fileName);
#if UNITY_EDITOR
			jsonString = GameResourceLoader.Instance.LoadJsonFileInPersistent(path);
#else
			//Load From PersistentPath
#endif
			List<Name2Path> pairs = JsonConvert.DeserializeObject<List<Name2Path>>(jsonString);
			dicPathMap = new Dictionary<string, string>();
			for (int i = 0; i < pairs.Count; i++)
			{
				if (dicPathMap.ContainsKey(pairs[i].Name))
				{
					Debug.LogError($"添加重复名称 ：{pairs[i].Name}");
				}
				else
				{
					dicPathMap.Add(pairs[i].Name, pairs[i].Path);
				}
			}
		}

		protected Dictionary<string, string> dicPathMap;
	}
}